﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.ApplicationBlocks.Data;
using System.Configuration;

namespace ModuleDuoc
{
    public partial class frmXuatThuocBH : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        public frmXuatThuocBH()
        {
            InitializeComponent();
            dgvLeft.AutoGenerateColumns = false;
            dgvChiTietBH.AutoGenerateColumns = false;
        }
        string sql;
        private static string _xuat;
       // string _xuatCa;
        DataTable dt = new DataTable();

        private void frmXuatThuocBH_Load(object sender, EventArgs e)
        {
            getMaTheByStatus();
        }

        private void getMaTheByStatus()
        {
            string _status = "0";
            if (radChuaXuat.Checked == true)
            {
                _status = "1";
            }
            else if (radDaXuat.Checked == true)
            {
                _status = "2";
            }
            try
            {
                sql = "SELECT BHYT_Code FROM DM_MedDetails " +
                                "WHERE HD_Status = "+_status+" GROUP BY BHYT_Code";

                dt = new DataTable();
                dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionStringBH"], CommandType.Text, sql).Tables[0];
                dgvLeft.DataSource = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi lấy dữ liệu mã thẻ!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void getByMaThe()
        {
            try
            {
                sql = "SELECT MaThuoc, TenThuoc, DVT, Soluong, LieuDung, [SID], HD_Status " +
                                "FROM DM_MedDetails " +
                                "WHERE BHYT_Code = N'" + dgvLeft.CurrentRow.Cells["BHYT_Code"].Value.ToString() + "' ";

                dt = new DataTable();
                dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionStringBH"], CommandType.Text, sql).Tables[0];
                dgvChiTietBH.DataSource = dt;
                txtBHYT.Text = dgvLeft.CurrentRow.Cells["BHYT_Code"].Value.ToString();
                txtSID.Text = dt.Rows[0]["SID"].ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi lấy dữ liệu bởi mã thẻ!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void dgvLeft_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            getByMaThe();
            itmXuatCaDon.Visible = true;
            itmXuatThuocNay.Visible = true;
        }

        private void itmXuatCaDon_Click(object sender, EventArgs e)
        {
            if (dgvChiTietBH.Rows.Count > 0)
            {
                _xuat = "2";
                xuatCaDon();
                getMaTheByStatus();
                dgvChiTietBH.DataSource = null;
                dgvChiTietBH.Refresh();
            }
        }

        private void itmXuatThuocNay_Click(object sender, EventArgs e)
        {
            if (dgvChiTietBH.Rows.Count > 0)
            {
                _xuat = "2";
                xuatThuocNay();
                getMaTheByStatus();
                dgvChiTietBH.DataSource = null;
                dgvChiTietBH.Refresh();
            }
        }

        private void xuatThuocNay()
        {
            try
            {
                sql = "UPDATE [ME_Insurance].[dbo].[DM_MedDetails] " +
                           "SET [HD_Status] = "+_xuat+" " +
                        "WHERE MaThuoc = "+dgvChiTietBH.CurrentRow.Cells["MaThuoc"].Value.ToString()+
                        " AND BHYT_Code = '" + txtBHYT.Text.Trim() + "' AND [SID] = '"+txtSID.Text.Trim()+"'";
                                
                SqlHelper.ExecuteNonQuery(ConfigurationManager.AppSettings["Main.ConnectionStringBH"], CommandType.Text, sql);
                MessageBox.Show("Cập nhật thành công!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi cập nhật dữ liệu!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void xuatCaDon()
        {
            try
            {
                sql = "UPDATE [ME_Insurance].[dbo].[DM_MedDetails] " +
                           "SET [HD_Status] = " + _xuat + " " +
                        "WHERE BHYT_Code = '" + txtBHYT.Text.Trim() + "' AND [SID] = '" + txtSID.Text.Trim() + "'";

                SqlHelper.ExecuteNonQuery(ConfigurationManager.AppSettings["Main.ConnectionStringBH"], CommandType.Text, sql);
                MessageBox.Show("Cập nhật thành công!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi cập nhật dữ liệu!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void txtMaBH_TextChanged(object sender, EventArgs e)
        {
            searchLikeMaThe();
        }
        private void searchLikeMaThe()
        {
            try
            {
                sql = "SELECT BHYT_Code FROM DM_MedDetails " +
                        "WHERE HD_Status = 1 AND BHYT_Code LIKE N'%"+txtMaBH.Text.Trim()+"%' " +
                        "GROUP BY BHYT_Code";

                dt = new DataTable();
                dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionStringBH"], CommandType.Text, sql).Tables[0];
                dgvLeft.DataSource = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi lấy dữ liệu bởi mã thẻ!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void itmKhongXuatThuocNay_Click(object sender, EventArgs e)
        {
            _xuat = "1";
            xuatThuocNay();
        }

        private void itmKhongXuatDonNay_Click(object sender, EventArgs e)
        {
            _xuat = "1";
            xuatCaDon();
        }

        private void radChuaXuat_CheckedChanged(object sender, EventArgs e)
        {
            getMaTheByStatus();
            strip1.Enabled = true;
            dgvChiTietBH.DataSource = null;
            dgvChiTietBH.Refresh();
        }

        private void radDaXuat_CheckedChanged(object sender, EventArgs e)
        {
            getMaTheByStatus();
            strip1.Enabled = false;
            dgvChiTietBH.DataSource = null;
            dgvChiTietBH.Refresh();
        }

        private void dgvLeft_KeyDown(object sender, KeyEventArgs e)
        {
            getMaTheByStatus();
        }
    }
}
